﻿using System;
using System.Collections.Generic;
using System.Data;

using Infrastructure.NetExtensions;
using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class WebFormSudentSalary : ABasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            LocalDateTime.SetIsraelFormat();
            SetGridView();
        }

        private void SetGridView()
        {
            List<BaseSalary> salaries = EBaseSalary.GetOrderedBaseSalaries();

            DataTable dt = new DataTable();

            dt.Columns.Add("שכר בסיס", typeof(decimal));
            dt.Columns.Add("תקף מתאריך", typeof(DateTime));
            dt.Columns.Add("עודכן בתאריך", typeof(DateTime));

            salaries.ForEach(
                (salary) =>
                {
                    DataRow row = dt.NewRow();
                    row[0] = salary.Salary;
                    row[1] = salary.ValidFromDate;
                    row[2] = salary.UpdatedOn;
                    dt.Rows.Add(row);
                });

            GridViewBaseSalary.DataSource = dt;
            GridViewBaseSalary.DataBind();
            GridViewBaseSalary.Visible = true;
        }

        protected void ButtonUpdateBaseSalary_Click(object sender, EventArgs e)
        {
            decimal salary;
            if (!decimal.TryParse(TextBoxBaseSalary.Text, out salary)) return;

            DateTime validFrom;
            if (!TextBoxValidFrom.GetSelectedDate(out validFrom)) return;

            BaseSalary baseSalary = new BaseSalary(salary, validFrom);
            EBaseSalary.Update(baseSalary);

            TextBoxBaseSalary.Text = string.Empty;
            TextBoxValidFrom.Text = string.Empty;

            SetGridView();
        }
    }
}
